package org.testis.gui.mvc.view.dynamicmap;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.DynamicMapControllerList;
import org.testis.gui.mvc.controller.dynamicmap.DynamicMapPopupController;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.util.LinkedList;
import javax.swing.JPopupMenu;
import org.orm.PersistentException;

/**
 *
 * @author Cahit Emir Erel
 */
public class DynamicMapPopupView extends JPopupMenu implements AbstractViewPanel {

    private static final long serialVersionUID = 2123181L;

    public DynamicMapPopupView() {
        initComponents();
        localInitialization();
    }

    public void localInitialization() {
    }

    private void initComponents() {

        jPopupMenu1 = this;
        addContainer = new javax.swing.JMenuItem();
        addObject = new javax.swing.JMenuItem();
        addBehaviour = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        delete = new javax.swing.JMenuItem();
        copy = new javax.swing.JMenuItem();
        oneToOneCopy = new javax.swing.JMenuItem();
        edit = new javax.swing.JMenuItem();
        clone = new javax.swing.JMenuItem();
        addObjectGroup = new javax.swing.JMenuItem();
        paste = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();

        close = new javax.swing.JMenuItem();
        copySuiteAtrr = new javax.swing.JMenuItem();
        searchAbstractId = new javax.swing.JMenuItem();
        validateTree = new javax.swing.JMenuItem();
        // Component 0
        addContainer.setText("Add Container");
        addContainer.setVisible(false);
        addContainer.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                AddContainerActionPerformed(evt);
            }
        });
        jPopupMenu1.add(addContainer);


        // Component 1
        addObjectGroup.setText("Add ObjectGroup");
        addObjectGroup.setVisible(false);
        addObjectGroup.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                AddObjectGroupActionPerformed(evt);
            }
        });
        jPopupMenu1.add(addObjectGroup);


        // Component 2
        addObject.setText("Add Object");
        addObject.setVisible(false);
        addObject.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                AddObjectActionPerformed(evt);
            }
        });
        jPopupMenu1.add(addObject);


        // Component 3
        addBehaviour.setText("Add Behaviour");
        addBehaviour.setVisible(false);
        addBehaviour.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                AddBehaviourActionPerformed(evt);
            }
        });
        jPopupMenu1.add(addBehaviour);


        // Component 4
        jPopupMenu1.add(jSeparator1);


        // Component 5
        delete.setText("Delete");
        delete.setVisible(false);
        delete.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                DeleteActionPerformed(evt);
            }
        });
        jPopupMenu1.add(delete);


        // Component 6
        edit.setText("Edit");
        edit.setVisible(false);
        edit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                EditActionPerformed(evt);
            }
        });
        jPopupMenu1.add(edit);


        // Component 7
        clone.setText("Clone");
        clone.setVisible(false);
        clone.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                CloneActionPerformed(evt);
            }
        });
        jPopupMenu1.add(clone);


        // Component 8
        copy.setText("Copy");
        copy.setVisible(false);
        copy.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                CopyActionPerformed(evt);
            }
        });
        jPopupMenu1.add(copy);

        // Component 9
        oneToOneCopy.setText("1-to-1 Copy");
        oneToOneCopy.setVisible(false);
        oneToOneCopy.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                oneToOneCopyActionPerformed(evt);
            }
        });
        jPopupMenu1.add(oneToOneCopy);


        // Component 10
        paste.setText("Paste");
        paste.setVisible(false);
        paste.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                try {
                    PasteActionPerformed(evt);
                } catch (PersistentException ex) {
                    ex.printStackTrace();
                }
                paste.setVisible(false);
            }
        });
        jPopupMenu1.add(paste);


        // Component 11
        jPopupMenu1.add(jSeparator2);

        //component 12
        copySuiteAtrr.setText("Copy Attributes");
        copySuiteAtrr.setVisible(false);
        copySuiteAtrr.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                CopyAttributesActionPerformed(evt);
            }
        });

        jPopupMenu1.add(copySuiteAtrr);

        //component 13
        searchAbstractId.setText("Search");
        searchAbstractId.setVisible(false);
        searchAbstractId.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                search(evt);
            }
        });
        jPopupMenu1.add(searchAbstractId);

        //component 14
        validateTree.setText("Validate Node");
        validateTree.setVisible(false);
        validateTree.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                validateTree(evt);
            }
        });
        jPopupMenu1.add(validateTree);

        //component 16
        close.setText("Close");
        close.setVisible(false);
        close.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                CloseActionPerformed(evt);
            }
        });
        jPopupMenu1.add(close);
    }// </editor-fold>

    private void search(ActionEvent evt) {
        String result = javax.swing.JOptionPane.showInputDialog("Enter Value:");
        if (result == null) {
            return;
        }
        if (!result.equals("")) {
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationChangeLastSearch(result.trim());
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                    operationFindNextNodeByText();
        }
    }

    private void validateTree(ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationChangeLastSearch("");
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationValidateTree();
    }

    private void AddContainerActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapController().
                operationAddDynamicMap("Container");
    }

    private void AddObjectActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapController().
                operationAddDynamicMap("Object");
    }

    private void AddBehaviourActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapController().
                operationAddDynamicMap("Behaviour");
    }

    private void AddObjectGroupActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapController().
                operationAddDynamicMap("ObjectGroup");
    }

    private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {
        int result =
                javax.swing.JOptionPane.showConfirmDialog(null,
                "Confirmation",
                "Really delete this node?",
                javax.swing.JOptionPane.YES_NO_CANCEL_OPTION,
                javax.swing.JOptionPane.QUESTION_MESSAGE);
        if (result == javax.swing.JOptionPane.YES_OPTION) {
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                    operationDeleteTreeNode();
        } else if (result == javax.swing.JOptionPane.NO_OPTION) {
        } else if (result == javax.swing.JOptionPane.CANCEL_OPTION) {
        }
    }

    private void EditActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapController().
                operationEditMapObject(DynamicMapControllerList.newInstance().
                getDynamicMapTreeController().
                operationGetSelectedDesignPart());
        DynamicMapControllerList.newInstance().getDynamicMapController().
                openWindow();
    }

    private void CloneActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapPopupController().
                operationSetNode(DynamicMapControllerList.newInstance().
                getDynamicMapTreeController().
                operationGetSelectedNode(),
                "Paste (Clone)");
    }

    private void CopyAttributesActionPerformed(ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapPopupController().
                operationSetNode(DynamicMapControllerList.newInstance().
                getDynamicMapTreeController().
                operationGetSelectedNode(),
                "Paste Attributes");
    }

    private void CopyActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapPopupController().
                operationSetNode(DynamicMapControllerList.newInstance().
                getDynamicMapTreeController().
                operationGetSelectedNode(),
                "Paste (Copy)");
    }

    private void oneToOneCopyActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapPopupController().
                operationSetNode(DynamicMapControllerList.newInstance().
                getDynamicMapTreeController().
                operationGetSelectedNode(),
                "Paste (1-to-1 Copy)");
    }

    private void PasteActionPerformed(java.awt.event.ActionEvent evt) throws PersistentException {
        if (paste.getText().equals("Paste (Clone)")) {
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                    operationPasteClone(DynamicMapControllerList.newInstance().
                    getDynamicMapPopupController().
                    operationGetNode(),
                    DynamicMapControllerList.newInstance().
                    getDynamicMapTreeController().
                    operationGetSelectedNode());
        } else if (paste.getText().equals("Paste (Copy)")) {
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                    operationPasteCopy(DynamicMapControllerList.newInstance().
                    getDynamicMapPopupController().
                    operationGetNode(),
                    DynamicMapControllerList.newInstance().
                    getDynamicMapTreeController().
                    operationGetSelectedNode(),
                    false);
        } else if (paste.getText().equals("Paste (1-to-1 Copy)")) {
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                    operationPasteCopy(DynamicMapControllerList.newInstance().
                    getDynamicMapPopupController().
                    operationGetNode(),
                    DynamicMapControllerList.newInstance().
                    getDynamicMapTreeController().
                    operationGetSelectedNode(),
                    true);
        } else if (paste.getText().equals("Paste Attributes")) {
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                    operationPasteAttributes(DynamicMapControllerList.newInstance().
                    getDynamicMapPopupController().
                    operationGetNode(),
                    DynamicMapControllerList.newInstance().
                    getDynamicMapTreeController().
                    operationGetSelectedNode(),
                    true);
        } else {
            return;
        }
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                operationRefreshTree();
    }

    private void CloseActionPerformed(java.awt.event.ActionEvent evt) {
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                operationCloseTree();
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        //System.out.println(evt.getPropertyName());
        boolean value = false;
        String event = evt.getNewValue().toString();
        if (event.equals("true")) {
            value = true;
        } else if (event.equals("false")) {
            value = false;
        } else {
        }
        if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_CONTAINER_VISIBILITY)) {
            addContainer.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_OBJECT_GROUP_VISIBILITY)) {
            addObjectGroup.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_OBJECT_VISIBILITY)) {
            addObject.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_BEHAVIOUR_VISIBILITY)) {
            addBehaviour.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_DELETE_VISIBILITY)) {
            delete.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_EDIT_VISIBILITY)) {
            edit.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_CLONE_VISIBILITY)) {
            clone.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_COPY_VISIBILITY)) {
            copy.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_1_TO_1_COPY_VISIBILITY)) {
            oneToOneCopy.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_PASTE_CLONE_VISIBILITY)) {
            paste.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_COPY_SUITE_ATTRIBUTES_VISIBILITY)) {
            copySuiteAtrr.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_SEARCH_ABSID_TREE_VISIBILITY)) {
            searchAbstractId.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_CLOSE_TREE_VISIBILITY)) {
            close.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.SET_POPUP_MENU_VALIDATE_TREE_VISIBILITY)) {
            validateTree.setVisible(value);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.OPEN_POPUP_MENU)) {
            LinkedList coordinates = (LinkedList) evt.getNewValue();
            Component component = (Component) coordinates.get(0);
            Integer mouseX = (Integer) coordinates.get(1);
            Integer mouseY = (Integer) coordinates.get(2);
            jPopupMenu1.show(component,
                    mouseX,
                    mouseY);
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.NODE_EDIT)) {
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.CHANGE_PASTE_LABEL)) {
            paste.setText(evt.getNewValue().toString());
            paste.repaint();
        } else if (evt.getPropertyName().equals(
                DynamicMapPopupController.NODE_DEPENDENCY)) {
        } else if (evt.getPropertyName().equals(DynamicMapPopupController.NODE)) {
        }
    }
    private javax.swing.JMenuItem addContainer;
    private javax.swing.JMenuItem addObject;
    private javax.swing.JMenuItem addBehaviour;
    private javax.swing.JMenuItem delete;
    private javax.swing.JMenuItem edit;
    private javax.swing.JMenuItem clone;
    private javax.swing.JMenuItem addObjectGroup;
    private javax.swing.JMenuItem paste;
    private javax.swing.JMenuItem close;
    private javax.swing.JMenuItem copy;
    private javax.swing.JMenuItem oneToOneCopy;
    private javax.swing.JMenuItem searchAbstractId;
    private javax.swing.JMenuItem validateTree;
    private javax.swing.JMenuItem copySuiteAtrr;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
}
